package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.ResultMenu;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

import java.util.List;

/**
 *
 */
@Service
@Slf4j
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;


    /**
     * 根据用户查询所有登录列表
     *
     * @param roles
     * @return
     */
    @Override
    public LinkedHashSet<ResultMenu> findAllMenuByRole(List<Role> roles) {
        log.info("[用户当前拥有的角色为]:{}", roles);
        //进行菜单去重和保持顺序
        LinkedHashSet<ResultMenu> resultMenuLinkedHashSet = new LinkedHashSet<>();
        //伪造个工作台假数据
        ResultMenu resultMenuWork = new ResultMenu("1","工作台","fa-dashboard","",null);
        resultMenuLinkedHashSet.add(resultMenuWork);
        for (Role role : roles) {
            //获取所有一级菜单
            List<Menu> menus = menuDao.findAllMenuByRoleId(role.getId());
            for (Menu menu : menus) {
                //一级目录存放pojo类
                ResultMenu resultMenu = new ResultMenu();

                resultMenu.setPath(menu.getPath());
                resultMenu.setTitle(menu.getName());
                resultMenu.setIcon(menu.getIcon());

                //根据path查出对应菜单的二级菜单放入里面menu的pojo里
                String oidPath = menu.getPath();
                String path = "/" + oidPath;
                List<Menu> menus2 = menuDao.findAllMenu2ByPath(role.getId(), path);
                //创建List集合后，存放的是二级菜单数据
                List<ResultMenu> resultMenuList = new ArrayList<>();
                for (Menu menu2 : menus2) {
                    ResultMenu resultMenu2 = new ResultMenu();
                    resultMenu2.setPath(menu2.getPath());
                    resultMenu2.setTitle(menu2.getName());
                    resultMenu2.setLinkUrl(menu2.getLinkUrl());
                    resultMenuList.add(resultMenu2);
                }
                //将所有的二级菜单集合放入一级菜单中
                resultMenu.setChildren(resultMenuList);
                //添加所有菜单到返回集合中，进行去重操作
                resultMenuLinkedHashSet.add(resultMenu);
            }
        }
        log.info("[返回的菜单数据]:{}", resultMenuLinkedHashSet);
        return resultMenuLinkedHashSet;
    }


    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[分页查询]data:{}", queryPageBean);
        //设置分页查询参数
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //查询page
        Page<Menu> page = menuDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void add(Menu menu) {
        menuDao.insert(menu);
    }

    @Override
    public void deleteById(Integer id) {
        menuDao.deleteById(id);
    }

    @Override
    public List<Menu> findAll() {
       return menuDao.selectById();
    }

    @Override
    public void update(Menu menu) {
        menuDao.update(menu);
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.selectByIdWithMenu(id);
    }

}
